package com.gm.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpSession;

/**
 * @Author Administrator
 * @Date 2020/6/2 10:58
 **/
@Controller
public class LoginController {

//    @RequestMapping({"/","login.html"})
//    public String doPage(){
//        return "login";
//    }

//    @RequestMapping(value = "/user/login",method = RequestMethod.POST)
    @PostMapping("/user/login")
    public String doLogin(@RequestParam("username") String username,
                          @RequestParam("password") String password,
                          Model model, HttpSession session){

        if (!StringUtils.isEmpty(username) && "666666".equals(password)){
            //登录成功
            session.setAttribute("user",username);
            return "redirect:/main.html";
        }else {
            model.addAttribute("msg","密码错误");
            return "login";
        }

    }
//    退出登录
    @GetMapping("/user/logout")
    public String logOut(HttpSession session){
        session.removeAttribute("user");
        return "login";
    }

}
